package com.tubruk.trackme;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.Point;
import android.graphics.RectF;
import android.location.Location;
import android.util.Log;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;
import com.google.android.maps.MyLocationOverlay;
import com.google.android.maps.Projection;

public class MyTrackLocationOverlay extends MyLocationOverlay {
	
	private int mRadius = 4;
	private int mode = 0;
	private int defaultColor;
	private GeoPoint gp1;
	private GeoPoint gp2;
	
	public MyTrackLocationOverlay(Context context, MapView mapView) {
		super(context, mapView);
	}
	
	public MyTrackLocationOverlay(Context context, MapView mapView, GeoPoint prevPoint, int mode) {
		this(context, mapView);
		this.gp1 = prevPoint;
		this.mode = mode;
	}

	@Override
	public synchronized void onLocationChanged(Location location) {
		super.onLocationChanged(location);
		
		Double latitude = location.getLatitude() * 1E6;
		Double longitude = location.getLongitude() * 1E6;
		gp2 = new GeoPoint(latitude.intValue(), longitude.intValue());
		
		// we got new location info. lets display it in the textview
		String s = "";
		s += "Time: " + location.getTime() + "\n";
		s += "\tLatitude:  " + location.getLatitude() + "\n";
		s += "\tLongitude: " + location.getLongitude() + "\n";
		s += "\tAccuracy:  " + location.getAccuracy() + "\n";
		s += "\tSpeed:     " + location.getSpeed() + "\n";

		Log.d("LOCATION UPDATE: ", s);
	}
	
	@Override
	public boolean draw(Canvas canvas, MapView mapView, boolean shadow,
			long when) {
		Projection projection = mapView.getProjection();
		if (shadow == false) {
			Paint paint = new Paint();
			//paint.setStrokeWidth(5);
			//paint.setAlpha(120);
			paint.setAntiAlias(true);
			
			paint.setDither(true);
	        paint.setColor(Color.RED);
	        paint.setStyle(Paint.Style.FILL_AND_STROKE);
	        paint.setStrokeJoin(Paint.Join.ROUND);
	        paint.setStrokeCap(Paint.Cap.ROUND);
	        paint.setStrokeWidth(2);
			
	        Point point = new Point();
	        if (gp1 != null) {
	        	projection.toPixels(gp1, point);
	        }
	        
	        Point point2 = new Point();
	        if (gp2 != null) {
				projection.toPixels(gp2, point2);
	        }
			
			Path mPath = new Path();
			
			// mode=1&#65306;start
			if (mode == 1) {
				if (defaultColor == 999)
					paint.setColor(Color.BLUE);
				else
					paint.setColor(defaultColor);
				
				//mPath.moveTo(point.x, point.y);
				RectF oval = new RectF(point.x - mRadius, point.y - mRadius,
						point.x + mRadius, point.y + mRadius);
				// start point
				canvas.drawOval(oval, paint);
				//canvas.drawPath(mPath, paint);
			}
			// mode=2&#65306;path
			else if (mode == 2) {
				if (defaultColor == 999)
					paint.setColor(Color.RED);
				else
					paint.setColor(defaultColor);
				
				mPath.moveTo(point.x, point.y);
				mPath.lineTo(point2.x, point2.y);
				
				RectF oval = new RectF(point2.x - (mRadius - 2), point2.y - (mRadius - 2),
						point2.x + (mRadius - 2), point2.y + (mRadius - 2));
				canvas.drawOval(oval, paint);
				
				//canvas.drawLine(point.x, point.y, point2.x, point2.y, paint);
				canvas.drawPath(mPath, paint);
			}
			/* mode=3&#65306;end */
			else if (mode == 3) {
				/* the last path */
				//mPath.moveTo(point2.x, point2.y);
				canvas.drawLine(point.x, point.y, point2.x, point2.y, paint);
				RectF oval = new RectF(point2.x - mRadius, point2.y - mRadius,
						point2.x + mRadius, point2.y + mRadius);
				paint.setColor(Color.BLUE);
				canvas.drawOval(oval, paint);
			}
		}
		return super.draw(canvas, mapView, shadow, when); 
	}
}
